package com.blackman.skcrawler.data.service;

import com.blackman.skcrawler.core.crawler.po.WormHomePage;
import com.blackman.skcrawler.data.mapper.WormHomePageMapper;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 爬虫起始页serviceImpl业务实现
 * @author staywood
 * @date 2018-04-18 13:25
 * @(C)  Copyright 
 */
@Service("wormHomePageService")
public class WormHomePageServiceImpl extends BaseServiceImpl<WormHomePageMapper, WormHomePage>{

    /**
     * 新增或更新
     * @param urlList
     */
    public void addOrUpdate(List<String> urlList) {
        if(urlList == null || urlList.size() == 0){
            return;
        }
        List<WormHomePage> homePageList = new ArrayList<WormHomePage>();
        for(int i = 0;i < urlList.size();i++){
            String url = urlList.get(i);
            WormHomePage wormHomePage = new WormHomePage();
            wormHomePage.setHomePageUrl(url);
            int count = selectCountByEntity(wormHomePage);
            if(count != 0){
                continue;
            }
            wormHomePage.setHomePageUrl(url);
            wormHomePage.setCreateTime(new Date());
            wormHomePage.setModifyTime(new Date());
            wormHomePage.setEnabled(false);
            homePageList.add(wormHomePage);
        }
        if(homePageList.size() == 0){
            return;
        }
        insertBatch(homePageList);
    }
}
